using System;
using System.Linq;
using CommandSystem;
using Exiled.API.Features;
using RgCore.PlayerHud;

namespace RgCore.Commands
{
    public class PrivateChatCommand : ICommand, IUsageProvider
    {
        public string[] Aliases => new string[] { "pbc" };

        public string Command => "playerbroadcast";

        public string Description => "给单个玩家发送公告";

        public string[] Usage => new string[]
        {
            "玩家id",
            "时间(s)",
            "内容",
        };

        public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
        {
            if (!sender.CheckPermission(PlayerPermissions.Broadcasting))
            {
                response = "你没有使用全服公告的权限！";
                return false;
            }
            if (arguments.Count < 3)
            {
                response = "用法：pbc <玩家id> <时长(秒)> <内容> --请不要输入太多内容！";
                return false;
            }

            if (!ushort.TryParse(arguments.At(1), out ushort sec))
            {
                response = "请输入一个合法的秒数";
                return false;
            }
            string text = string.Empty;
            foreach (string str in arguments.Skip(2))
                text += str + " ";
            string fails = string.Empty;
            string[] players = arguments.At(0).Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string a in players)
            {
                if (int.TryParse(a, out int id))
                {
                    if (Player.Get(id) != null)
                    {
                        Player.Get(id).SendFormattedMsg("admin_msg", "<color=#FFB5C5>管理私信</color>",
                            $"<color=#00FF00>{((CommandSender)sender).Nickname}</color>: {text}", sec, MsgType.Message,
                            ScreenLocation.CenterTop, true);
                    }
                    else
                    {
                        fails += a + ", ";
                    }
                }
            }
            Player.Get(sender).SendFormattedMsg("admin_msg", "<color=#FFB5C5>私信预览</color>",
                $"<color=#00FF00>{((CommandSender)sender).Nickname}</color>: {text}", sec, MsgType.Message,
                ScreenLocation.CenterTop, true);
            response = "已发送成功！" + (string.IsNullOrEmpty(fails) ? $"以下ID未能发送：{fails}" : string.Empty);
            return true;
        }
    }
}